package my.market.tools.verifyCode;

import javax.servlet.*;
import javax.servlet.http.*;

import com.mchange.v2.c3p0.impl.NewPooledConnection;

import java.io.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.image.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

/**
 * Servlet implementation class VerifyCodeGen
 */
public class VerifyCodeGen extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private static int width = 60;
    private static int height = 20;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public VerifyCodeGen() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		response.setContentType("image/jpeg");
		ServletOutputStream servletOutputStream = response.getOutputStream();
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		char [] rands = generateCheckCode();
		drawBackground(g);
		drawRands(g, rands);
		ByteArrayOutputStream bOutputStream = new ByteArrayOutputStream();
		ImageIO.write(image, "JPEG", bOutputStream);
		byte [] buf = bOutputStream.toByteArray();
		response.setContentLength(buf.length);
		servletOutputStream.write(buf);
		bOutputStream.close();
		servletOutputStream.close();
		session.setAttribute("verify_code", new String(rands));
	}
	
	private char [] generateCheckCode() {
		String chars = "0123456789abcdefghijklmnopqrstuvwxyz";
		char [] rands = new char[4];
		for(int i = 0; i < 4 ; i++) {
			int rand = (int)(Math.random() * 36);
			rands[i] = chars.charAt(rand); 
		}
		return rands;
	}
	
	private void drawRands(Graphics g, char [] rands ) {
		g.setColor(Color.BLACK);
		g.setFont(new Font(null, Font.ITALIC|Font.BOLD, 18));
		g.drawString("" + rands[0], 1, 17);
		g.drawString("" + rands[1], 16, 15);
		g.drawString("" + rands[2], 31, 18);
		g.drawString("" + rands[3], 46, 16);
	}
	
	private void drawBackground(Graphics g) {
		g.setColor(new Color(0xCDCDCD));
		g.fillRect(0, 0, width, height);
		for(int i = 0; i < 120; i++) {
			int x = (int) (Math.random() * width);
			int y = (int) (Math.random() * height);
			int green = (int) (Math.random() * 255);
			int blue = (int) (Math.random() * 255);
			int red = (int) (Math.random() * 255);
			g.setColor(new Color(red, green, blue));
			g.drawOval(x, y, 1, 0);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
